#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/5/5 8:38
# @Author  : LiShan
# @Email   : lishan_1997@126.com
# @File    : draw.py
# @Note    : this is note

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


# 绘制奖励曲线
file = "../model/train_record.txt"
names = ["delay", "reward", "step", "epsilon"]
data = pd.read_csv(file, error_bad_lines=False, sep="\s+", names=names)
delay = list(data["delay"].values)
reward = list(data["reward"].values)
epsilon = list(data["epsilon"].values)
"""延误"""
# 绘图
x = np.linspace(0, len(delay), len(delay))
plt.plot(x, delay)
# 设置坐标轴范围
plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
plt.ylabel("Delay", fontproperties="Times New Roman", size=10.5)
# 设置网格
plt.grid()
# 设置图例
legend = ["delay"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
plt.title("The Delay Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig("../model/delay.svg")
# 关闭绘图
plt.close()

"""奖赏"""
# 绘图
x = np.linspace(0, len(reward), len(reward))
plt.plot(x, reward)
# 设置坐标轴范围
plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
plt.ylabel("Reward", fontproperties="Times New Roman", size=10.5)
# 设置网格
plt.grid()
# 设置图例
legend = ["reward"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
plt.title("The Reward Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig("../model/reward.svg")
# 关闭绘图
plt.close()

"""epsilon"""
# 绘图
x = np.linspace(0, len(epsilon), len(epsilon))
plt.plot(x, epsilon)
# 设置坐标轴范围
plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
plt.ylabel("Epsilon", fontproperties="Times New Roman", size=10.5)
# 设置网格
plt.grid()
# 设置图例
legend = ["epsilon"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
plt.title("The Epsilon Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig("../model/epsilon.svg")
# 关闭绘图
plt.close()

"""衰减图"""
# 绘图
x = np.linspace(0, 100, 100)
plt.plot(x, 0.95*0.97**x)
# 设置坐标轴范围
plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
plt.ylabel("Epsilon", fontproperties="Times New Roman", size=10.5)
# 设置网格
plt.grid()
# 设置图例
legend = ["epsilon"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
plt.title("The Epsilon Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig("../model/demo.svg")
# 关闭绘图
plt.close()

"""损失函数图"""
# 绘制奖励曲线
file = "../model/loss.txt"
data = pd.read_csv(file, error_bad_lines=False, sep=",", header=None, nrows=1)
data = data.values[:,:-1].tolist()[0]
# 绘图
x = np.linspace(0, len(data), len(data))
plt.plot(x, data)
# 设置坐标轴范围
plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
plt.ylabel("Loss", fontproperties="Times New Roman", size=10.5)
# 设置网格
plt.grid()
# 设置图例
legend = ["loss"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
plt.title("The Loss Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig("../model/loss.svg")
# 关闭绘图
plt.close()
